/**
 * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xin.xinny.kit;

import com.jfinal.core.Const;
import jodd.props.Props;
import jodd.props.PropsUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class JoddPropsKit {

	private static Props props = new Props();
	private static final ConcurrentHashMap<String, Props> map = new ConcurrentHashMap<String, Props>();

	private JoddPropsKit() {}

	public static Props use(String fileName) {
		return use(fileName, Const.DEFAULT_ENCODING);
	}

	public static Props use(String fileName, String encoding) {
		Props props = map.get(fileName);
		try {
			if (props == null) {
                props = new Props();
				props.load(JoddPropsKit.class.getResourceAsStream("/"+fileName),encoding);
                map.put(fileName, props);
				JoddPropsKit.props.load(JoddPropsKit.class.getResourceAsStream("/"+fileName),encoding);
            }
		} catch (IOException e) {
		}
		return props;
	}

//
//	private static Props use(File file) {
//		return use(file, Const.DEFAULT_ENCODING);
//	}
//
//
//	private static Props use(File file, String encoding) {
//		Props props = map.get(file.getName());
//		try {
//			if (props == null) {
//				props = new Props();
//				props.load(new FileInputStream(file),encoding);
//				JoddPropsKit.props.load(new FileInputStream(file),encoding);
//				map.put(file.getName(), props);
//			}
//		} catch (IOException e) {
//		}
//		return props;
//	}

	public static void clear() {
		props = null;
		map.clear();
	}

	public static Props getProps() {
		return props;
	}

	public static Props getProps(String fileName) {
		return map.get(fileName);
	}

	public static String get(String key) {
		return getProps().getValue(key);
	}

	public static String get(String key, String defaultValue) {
		return get(key)==null?defaultValue:get(key);
	}

	public static Integer getInt(String key) {
		return getProps().getIntegerValue(key);
	}

	public static Integer getInt(String key, Integer defaultValue) {
		return getInt(key)==null?defaultValue:getInt(key);
	}

	public static Long getLong(String key) {
		return getProps().getLongValue(key);
	}

	public static Long getLong(String key, Long defaultValue) {
		return getLong(key)==null?defaultValue:getLong(key);
	}

	public static Boolean getBoolean(String key) {
		return getProps().getBooleanValue(key);
	}

	public static Boolean getBoolean(String key, Boolean defaultValue) {
		return getBoolean(key)==null?defaultValue:getBoolean(key);
	}

	public static boolean containsKey(String key) {
		return getProps().getValue(key)!=null;
	}
}


